package com.yanqu.client;

import com.yanqu.client.db.DbMgr;
import com.yanqu.client.player.Player;
import com.yanqu.road.entity.servercenter.DbInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.vertx.core.Future;
import io.vertx.core.Vertx;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


public class JoinUnion {
    static int amount = 0;

    public static void main(String[] args) throws SQLException {
        int takeCount = 10;//Integer.valueOf(args[1]);
        int titleIdNeed = 10;
        int serverId = 16000001;
        List<String> ipPort = DbMgr.getGameWebIpPort(serverId);
        if (null == ipPort) {
            return;
        }
        DbInfo dbInfo = DbMgr.getGameServerDbInfo(serverId);
        if (null == dbInfo) {
            return;
        }
        List<String> openIds = DbMgr.getOpenIdBySql(titleIdNeed, takeCount, dbInfo);
        if (null == openIds) {
            System.out.println("取不到需要的openId！！");
            return;
        }

        Vertx vertx = Vertx.vertx();
        for (String openId : openIds) {
            Player player = new Player(ipPort.get(0), Integer.valueOf(ipPort.get(1)), openId, vertx, serverId);
            Future<Object> gameLogin = player.gameLogin();
            gameLogin.onSuccess(o -> {
                YanQuMessage yanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_UNION_JOIN, player.getUserId(),
                        UnionProto.UnionJoinReqMsg.newBuilder().setUnionUid(""));
                player.sendMessage(yanQuMessage);
                amount++;
                System.out.println("批量加入商会-" + openId + "----累计" + amount);
                player.sendMessage(YanQuMessageUtils.buildMessage(GameProtocol.S_PLAYER_LOGOUT, player.getUserId(),null));
                System.out.println(player.getUserId()+"退出");
            });
        }


        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                //todo 这里关闭连接
                System.out.println("收到关闭事件");
            }
        });
    }
}